/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: InventoryPanel.java 568 2010-07-25 20:56:20Z Harry $
 *   $LastChangedDate: 2010-07-25 20:56:20 +0000 (Sun, 25 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.entity.UnitModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;

/**
 *
 * @author Harry Nguyen
 */
public class InventoryPanel extends JPanel {

    public static final int MAX_ITEMS = 16;
    InventoryButton[] buttons;
    ButtonGroup buttonGroup;
    List<UnitModel> models;

    public InventoryPanel(List<UnitModel> models) {
        this.models = models;
        initComponents();
    }

    private void initComponents() {
        this.setOpaque(false);
        this.setLayout(new GridLayout(2, 8));

        buttonGroup = new ButtonGroup();
        buttons = new InventoryButton[models.size()];
        for (int i = 0; i < MAX_ITEMS; i++) {
            if (i < models.size()) {
                buttons[i] = new InventoryButton(models.get(i));
                buttons[i].setPreferredSize(new Dimension(InventoryButton.WIDTH, InventoryButton.HEIGHT));
                buttons[i].setSize(new Dimension(InventoryButton.WIDTH, InventoryButton.HEIGHT));
                buttonGroup.add(buttons[i]);
                this.add(buttons[i]);
            } else {
                TransparentPanel transparentPanel = new TransparentPanel();
                transparentPanel.setPreferredSize(new Dimension(InventoryButton.WIDTH, InventoryButton.HEIGHT));
                transparentPanel.setSize(new Dimension(InventoryButton.WIDTH, InventoryButton.HEIGHT));
                this.add(transparentPanel);
            }
        }

//        for (int i = 0; i < models.size(); i++) {
//            buttons[i] = new InventoryButton(models.get(i));
//            buttons[i].setPreferredSize(new Dimension(80, 75));
//            buttons[i].setSize(new Dimension(80, 75));
//            buttonGroup.add(buttons[i]);
//            this.add(buttons[i]);
//        }

        //this.setPreferredSize(new Dimension((models.size()),));




//        GroupLayout layout = new GroupLayout(this);
//        this.setLayout(layout);
//        layout.setAutoCreateGaps(true);
//        layout.setAutoCreateContainerGaps(true);
//        layout.setHorizontalGroup(
//                layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(buttons[0], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[4], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(buttons[1], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[5], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(buttons[2], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[6], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(buttons[3], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[7], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));
//        layout.setVerticalGroup(
//                layout.createSequentialGroup().addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(buttons[0], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[1], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[2], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[3], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)).addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(buttons[4], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[5], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[6], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addComponent(buttons[7], GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)));
        update();
    }

    public void update() {
    }

    public void addActionListener(ActionListener obj, String actionCommand) {
        for (int i = 0; i < buttons.length; i++) {
            buttons[i].addActionListener(obj);
            buttons[i].setActionCommand(actionCommand);
        }
    }
}
